
var vm = new Vue({
    el: "#homeVue",
    data: {
        lists:[],           //商家列表
        discounts:[],       //优惠信息
        categories:[],      //分类列表
        brands:[],          //品牌列表
        bannerImages:[],    //轮播图
        user_id:'',
        web_token:'',
        searchs: '',
        city: '',
        classify:'',
        classify_id:'',
        discount: true,
        type: false,
        brand: false,
        loading: true,
        pageNo: 1,
        paginate:'',
        pageStart: 1,
        pageEnd: 100,
        pageCount:'',
        page_no: 1,
        oss_url:oss_url,
        nodata:'',
        classification_id:'',
        brand_id:''

    },
    methods: {
        dataDetails:function (id) {
            this.type           = true;
            this.classify_id    = id;
        },
        hiddenDetail:function () {
            // $('.categories_list').find('li').children('a').removeClass('active');
            // $('.categories_list').find('li').find('img').attr('src',"/image/userweb/slicesImg/list-arrow.png");
            this.type           = false;
        },
        classification:function (index,id) {
            this.discount       = true;
            if(index == 1){
                this.brand      = false;
                this.type       = false;
                this.classify   = 'category_id,'+id;
                this.classification_id = 'category_id,'+id;
            }
            if(index == 2){
                this.brand      = false;
                this.type       = false;
                this.classify   = 'type_id,'+id;
                this.classification_id = 'type_id,'+id;
            }
            if(index == 3){
                this.classify   = 'small_type_id,'+id;
                this.classification_id = 'small_type_id,'+id;
                this.brand      = true;
                this.type       = false;
            }
            if(index == 4){
                this.brand_id   = ',brand_id,'+id;
                this.classify   = this.classification_id+this.brand_id;
            }
            if (index == 5){
                $('.categories_list').find('li').children('a').removeClass('active');
                $('.categories_list').find('li').find('img').attr('src',"/image/userweb/slicesImg/list-arrow.png");
                this.classify   = '';
                this.brand      = false;
                this.type       = false;
            }
            this.searchs  = '';
            this.pageNo   = 1;
            this.req();
        },
        chang:function (index) {
            this.page_no = parseInt(this.page_no);
            if (index == 1){
                this.page_no -= 1;
                if (this.page_no<1){
                    this.page_no = 1;
                }
            }else if (index == 2){
                this.page_no += 1;
                if (this.page_no > this.discounts.pageCount){
                    this.page_no = this.discounts.pageCount;
                }
            }
            this.activity();
        },
        req: function () {
            this.loading = true;
            var url = '/userweb/Homepage/getList';
            var data = {
                user_id:    this.user_id,
                city:       this.city,
                search:     this.searchs,
                classify:   this.classify,
                page:       this.pageNo
            };
            this.$http.post(url, data, {
                emulateJSON: true
            }).then(function (res) {
                if (res.data.status == 200) {
                    this.lists      = res.data.supplier.data;
                    this.paginate   = res.data.paginate;
                    this.brands     = res.data.brands;
                    if (this.brands.length==0){
                        this.brand = false;
                    }else {
                        this.brand = true;
                    }
                    if (this.lists.length==0){
                        $('#noData').html(this.nodata);
                    }else {
                        $('#noData').html("");
                    }
                    var _this = this;
                    $.each(this.lists,function (i,v) {
                        _this.lists[i]['star']  = star(Math.round(v['star'] * Math.pow(10, 0)) / Math.pow(10, 0));
                        _this.lists[i]['image'] = _this.oss_url + _this.lists[i]['image'];
                    });
                    // console.log(this.lists);
                } else {
                    layer.msg(res.data.msg,{
                        skin:'winning-class',
                        time:3000
                    });
                }
               /* setTimeout(function () {
                    _this.loading = false;
                }, 2000);*/
               _this.loading = false;
            }, function (res) {

            });

        },
        ini: function () {
            var url = '/userweb/Homepage/getIni';
            var data = {
                user_id:    this.user_id,
                web_token:  this.web_token,
                page_no:    this.page_no
            };
            this.$http.post(url, data, {
                emulateJSON: true
            }).then(function (res) {
                if (res.data.status == 200) {
                    this.categories = res.data.categories;
                }else {
                    layer.msg(res.data.msg,{
                        skin:'winning-class',
                        time:3000
                    });
                }
            }, function (res) {

            });
        },
        activity:function () {
            var url = '/userweb/Homepage/discounts';
            var data = {
                user_id:    this.user_id,
                web_token:  this.web_token,
                page_no:    this.page_no
            };
            this.$http.post(url, data, {
                emulateJSON: true
            }).then(function (res) {
                if (res.data.status == 200) {
                    this.discounts  = res.data.discounts;
                    this.city       = res.data.city;
                }else {
                    layer.msg(res.data.msg,{
                        skin:'winning-class',
                        time:3000
                    });
                }
                for (var i=0;i<this.discounts.list.length;i++){
                    this.discounts.list[i]['image'] = this.oss_url + this.discounts.list[i]['image'];
                }
            }, function (res) {

            });
        }
    },
    mounted: function () {
        this.nodata += '<div class="my_no-data">';
        this.nodata += '<div class="no_data_tip">';
        this.nodata += '<img src="/image/userweb/slicesImg/no-data.png" alt="">';
        this.nodata += '<div class="no-data-text">';
        this.nodata += '<div class="title_zh">暂无数据</div>';
        this.nodata += '<div class="title_en">No data</div>';
        this.nodata += '</div>';
        this.nodata += '</div>';
        this.nodata += '</div>';
        //localStorage.removeItem('user_id');//清除.
        var str = location.href; //取得整个地址栏
        this.searchs    = getUrlParam(str,'search');
        this.user_id    = localStorage.getItem('user_id');
        this.user_name  = localStorage.getItem('user_name');
        this.city       = localStorage.getItem('city');
        if (!this.city){
            this.city           = 'Devon County';
            localStorage.city   = this.city;
        }
        //console.log(this.city);
        this.web_token  = localStorage.getItem('web_token');
        this.ini();
        this.activity();
        this.req();
    }
});
function AjaxPage(page) {
    vm.pageNo = page;
    vm.req();
}
function jump(){
    var page = $("#page_no").val();
    vm.pageNo = page;
    vm.req();
}